using System;
using System.Collections.Generic;
using Library;
using Library.Util;
using NHibernate;

namespace Library.Persistence.Cloud.Dao
{
	public class CloudFirmaKullaniciAyarDao : BaseEntity
	{
		private readonly string fromHql = " from CloudFirmaKullaniciAyar as t ";
		public void Insert(CloudFirmaKullaniciAyar dto)
		{
			BasePersistence.Insert(dto);
		}

		public void Update(CloudFirmaKullaniciAyar dto)
		{
			BasePersistence.Update(dto);
		}

		public void Delete(CloudFirmaKullaniciAyar dto)
		{
			BasePersistence.Delete(dto);
		}

		public void Delete(int firmaKullaniciAyarId)
		{
			CloudFirmaKullaniciAyar dto = new CloudFirmaKullaniciAyar();
			dto.FirmaKullaniciAyarId = firmaKullaniciAyarId;
			Delete(dto);
		}

		public CloudFirmaKullaniciAyar Get(int firmaKullaniciAyarId, LockMode lockMode)
		{
			return Get<CloudFirmaKullaniciAyar>(firmaKullaniciAyarId, lockMode);
		}

		public List<CloudFirmaKullaniciAyar> FindAll()
		{
			return Find<CloudFirmaKullaniciAyar>(fromHql, LockMode.Read);
		}

		public List<CloudFirmaKullaniciAyar> FindAll(string orderStatement)
		{
			return Find<CloudFirmaKullaniciAyar>(fromHql + " ORDER BY " + orderStatement, LockMode.Read);
		}

		public CloudFirmaKullaniciAyar FindByFirmaKullaniciIdAndAyarKodu(string firmaKullaniciId, string ayarKodu, LockMode lockMode)
		{
			string hql = fromHql;
			hql += @"WHERE 
t.FirmaKullaniciId = :firmaKullaniciId And t.AyarKodu = :ayarKodu";
			FinderParameter[] parameters = new FinderParameter[2];
			parameters[0] = new FinderParameter("parameter", "firmaKullaniciId", firmaKullaniciId);
			parameters[1] = new FinderParameter("parameter", "ayarKodu", ayarKodu);
			List<CloudFirmaKullaniciAyar> list = Find<CloudFirmaKullaniciAyar>(hql, parameters, lockMode);
			if (list == null || list.Count == 0)
				return null;
			if (list.Count > 1) {
				string exception = "";
				for (int i = 0; i < parameters.Length; i++)
					exception += parameters[i] + " ";
				throw new ApplicationException(AppMessage.Get("non-unique.result", "CloudFirmaKullaniciAyar.FindByFirmaKullaniciIdAndAyarKodu", exception));
			}
			return list[0];
		}

	}
}
